package com.wanmait.wanmarecruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.wanmarecruit.service.NoticeService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wanmarecruit.entity.Notice;
import com.wanmait.wanmarecruit.mapper.NoticeMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Resource
    private NoticeMapper noticeMapper;
    @Override
    public PageInfo<Notice> selectAllNoticeByUser(Integer userId,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Notice> notices = noticeMapper.selectAllNoticeByUser(userId);
        return new PageInfo<>(notices,3);
    }

    @Override
    public Integer getAllNoticeNotRead(Integer userId) {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getUserId,userId);
        queryWrapper.eq(Notice::getReader,false);
        return (int)this.count(queryWrapper);
    }

    @Override
    public void updateBatchIsReading(Integer[] ids) {
        List<Notice> notices = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            Notice notice = new Notice();
            notice.setId(ids[i]);
            notice.setReader(true);
            notices.add(notice);
        }
        this.updateBatchById(notices);
    }


    @Override
    public void updateNoticeIsReadingById(Integer id) {
        Notice notice = new Notice();
        notice.setId(id);
        notice.setReader(true);
        this.updateById(notice);
    }

    @Override
    public void deleteBatchByIds(Integer[] ids) {
        List<Notice> notices = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            Notice notice = new Notice();
            notice.setId(ids[i]);
            notices.add(notice);
        }
        this.removeBatchByIds(notices);
    }
}
